package com.yuexunit.finance.project.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.project.entity.BillReceivableEntity;
import com.yuexunit.finance.project.entity.lexicon.BillTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;


/**
 * @author wenwenlong
 */
@Implements(BillReceivableService.class)
public class BillReceivableServiceImpl implements BillReceivableService {

	@Override
	@Transactional
	public Long createBillReceivable(BillReceivableEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEnabled(Boolean.TRUE);
		PersistUtil.insert(entity);
		return entity.getBillReceivableId();
	}

	@Override
	@Transactional
	public void updateBillReceivable(BillReceivableEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void deleteBillReceivable(Long billReceivableId) {
		BillReceivableEntity entity = PersistUtil.get(BillReceivableEntity.class, billReceivableId);
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.update(entity);
	}

	@Override
	public BillReceivableEntity inquireBillReceivable(Long billReceivableId) {
		BillReceivableEntity entity = PersistUtil.get(BillReceivableEntity.class, billReceivableId);
		return entity;
	}

	@Override
	public PageList<BillReceivableEntity> inquireBillReceivablePageList(BillTypeEnum billType, Date startDate, Date endDate, String writeoffNumber,
			String payee, String drawee, Long moneyStatementId, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BillReceivableEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("billType", billType));
		
		if (startDate != null) {
			criteria.add(Restrictions.le("updateDateTime", startDate));
		}
		
		if (endDate != null) {
			criteria.add(Restrictions.ge("updateDateTime", endDate));
		}
		
		if (moneyStatementId != null) {
			criteria.add(Restrictions.eq("moneyStatementId", moneyStatementId));
		}
		
		if (StringUtil.isNotBlank(writeoffNumber)) {
			criteria.add(Restrictions.like("writeoffNumber", "%" + writeoffNumber + "%"));
		}
		
		if (StringUtil.isNotBlank(payee)) {
			criteria.add(Restrictions.like("payee", "%" + payee + "%"));
		}
		
		if (StringUtil.isNotBlank(drawee)) {
			criteria.add(Restrictions.like("drawee", "%" + drawee + "%"));
		}
		criteria.addOrder(Order.desc("updateDateTime"));
		
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<BillReceivableEntity> inquireBillReceivableList(Long moneyStatementId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BillReceivableEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("moneyStatementId", moneyStatementId));
		return PersistUtil.get(criteria);
	}
}
